﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using xKnight.Analyzing.BaseAnalyze;
using xKnight.Analyzing.EncodedXss;
using xKnight.Analyzing.SimpleXss;
using xKnight.Analyzing.Xss;
using xKnight.Attacking;
using xKnight.Models;

namespace xKnight.Analyzing
{
    public abstract class Analyzer
    {
        internal delegate void AnalyzeAsyncCaller();

        #region Fields

        protected AnalyzingSharedResource _analyzingSharedResource;

        #endregion

        #region Events

        public event AnalyzeStartedEventHandler AnalyzeStarted;
        public event AnalyzeCompletedEventHandler AnalyzeCompleted;
        public event AnalyzeAnnouncedEventHandler AnalyzeAnnounced;

        #endregion

        #region Constructors

        public Analyzer(Attack attack)
        {
            _analyzingSharedResource = CreateAnalyzingSharedResource(attack);
        }

        #endregion

        #region Interface

        public abstract void Analyze();

        public void AnalyzeAsync()
        {
            AnalyzeAsyncCaller asyncCaller = new AnalyzeAsyncCaller(Analyze);

            OnAnalyzeStarted();
            asyncCaller.BeginInvoke(new AsyncCallback(OnAnalyzeCompleted), null);
        }

        protected abstract AnalyzingSharedResource CreateAnalyzingSharedResource(Attack attack);

        #endregion

        #region Event Handlers

        protected void OnAnalyzeStarted()
        {
            if (AnalyzeStarted != null)
                AnalyzeStarted(this, new AnalyzeStartedEventArgs(DateTime.Now));
        }

        protected void OnAnalyzeCompleted(IAsyncResult result)
        {
            OnAnalyzeCompleted();
        }

        protected void OnAnalyzeCompleted()
        {
            if (AnalyzeCompleted != null)
                AnalyzeCompleted(this, new AnalyzeCompletedEventArgs(DateTime.Now));
        }

        protected void OnAnalyzeAnnounced(AnalyzeAnnounceItem announceItem)
        {
            if (AnalyzeAnnounced != null)
                AnalyzeAnnounced(this, new AnalyzeAnnouncedEventArgs(announceItem));
        }

        #endregion
    }
}
